package com.yc.boottxx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.boottxx.entity.Car;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 天下行租车-车辆表-10 Mapper 接口
 * </p>
 *
 * @author xl
 * @since 2024-05-07
 */
public interface CarMapper extends BaseMapper<Car> {

    @Select("<script>" +
            "SELECT * FROM txx_car" +
            "<where>" +
            "<if test='brand != null and brand != &quot;&quot;'>brand = #{brand} AND </if>" +
            "<if test='model != null and model != &quot;&quot;'>model LIKE CONCAT('%',#{model},'%') AND </if>" +
            "<choose>" +
            "<when test='price == 1'>price BETWEEN 0 AND 100</when>" +
            "<when test='price == 2'>price BETWEEN 101 AND 200</when>" +
            "<when test='price == 3'>price BETWEEN 201 AND 500</when>" +
            "<otherwise>price &gt; 500</otherwise>" +
            "</choose>" +
            "</where>" +
            "<if test='sort != null and sort != &quot;&quot;'>ORDER BY ${sort} </if>" +
            "<if test='sort != null and sort != &quot;&quot; and sc != null and sc != &quot;&quot;'>${sc} </if>" +
            "</script>")
    List<Car> select(Car car);

    @Select("select distinct brand from txx_car")
    public List<String> selectBrand();

    @Select("select * from txx_car where id=#{id}")
    //在xml中 是 <resultMap><result><assi...>
    //<resultMap>= @Results    @Result=<result>  <assi...>=@One
    @Results(
            id = "rm1", /*这个随便*/
            value = {
                    //id组件映射 => 提升缓存性能,建议配置
                    @Result(column = "id",property = "id",id = true),
                    // 一对多
                    @Result(column = "id",property = "photo",many = @Many(select="com.yc.boottxx.mapper.ImageMapper.getDetailImg"))
            }
    )
    public Car selectCarDetail(String id);


    @Insert("insert into txx_car values(null,#{brand},#{model},#{image},#{intro},#{price},4,#{state},now())")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int addCar(Car car);


    @Select("select * from txx_car where id=#{id}")
    List<Car> queryById(String id);
}
